<template>
	<view class="page" :style="{'--color': color}">
		<status-bar title='信息设置'></status-bar>
		<form @submit="Save">
			<template v-if='userinfo'>

				<view class="avatar-box ">
					<image class="avatar" :src="v_headimg?$imgUrls(v_headimg):staticUrl+'/images/jh_up/head.jpg'"
						mode="aspectFill"></image>

					<button open-type="chooseAvatar" type="default" class="AvatarBtn"
						@chooseavatar="bindchooseavatar"></button>
				</view>
				<view class="list" v-if='userinfo.list && userinfo.list.length > 0'>
					<block v-for="(item,index) in userinfo.list" :key='index'>
						<!-- 1单行输入 -->
						<view class="rows" v-if='item.vpType == 1'>
							<view class="item flex-box align-center" v-if="item.vpName == '姓名'">
								<text class="name" :class="{repuired: item.bixu == 1}">{{ item.vpName }}</text>
								<input type="nickname" name="nickname" class="flex-1 m-left40 wh size28 color_33"
									:placeholder="`请输入${item.vpName}`" placeholder-class="placeStyle_cc"
									v-model="item.value" />
							</view>
							<view class="item flex-box align-center" v-else>
								<text class="name" :class="{repuired: item.bixu == 1}">{{ item.vpName }}</text>
								<input type="text" :placeholder="'请输入'+item.vpName" v-model="item.value" class="flex"
									:disabled="item.lock==1?true:false" :class="'lock'+item.lock"
									placeholder-class="pla" />
							</view>
						</view>



						<!-- 2下拉选择 -->
						<picker mode="selector" :range="item.vpValue" @change="change(index,$event)"
							v-if='item.vpType == 2'>
							<view class="item flex-box align-center">
								<text class="name" :class="{repuired: item.bixu == 1}">{{ item.vpName }}</text>
								<input type="text" placeholder="请选择性别" v-model="item.value" class="flex"
									placeholder-class="pla" disabled :class="'lock'+item.lock" />
								<text class="iconfont icon-xiangyouxiayiye"></text>
							</view>
						</picker>

						<!-- 3文本输入 -->
						<view class="textarea-box" v-if='item.vpType == 3'>
							<text class="name" :class="{repuired: item.bixu == 1}">{{ item.vpName }}</text>
							<textarea type="text" :placeholder="'请输入'+item.vpName" v-model="item.value" class="flex"
								placeholder-class="pla" :disabled="item.lock==1?true:false"
								:class="'lock'+item.lock"></textarea>
						</view>

						<!-- 4复选框 -->
						<view class="check-box flex-box" v-if='item.vpType == 4'>
							<text class="name" :class="{repuired: item.bixu == 1}">{{ item.vpName }}</text>
							<view class="check-box-list flex-box flex">
								<view class="check" :class="{active: ck.checked}" v-for="(ck,i) in item.vpValue"
									:key='i' @click="check(index,i)">
									<view class="flex-box radio">
										<text class="icon-xuanzhong iconfont"></text>
									</view>
									<text class="txt" :class="'lock'+item.lock">{{ ck.val }}</text>
								</view>
							</view>
						</view>

						<!-- 5日期选择 -->
						<timepicker showType="date" @btnConfirm="btnConfirm" :index1="index" id='mypicker'
							ref='mypicker' v-if='item.vpType == 5'>
							<view class="item flex-box align-center">
								<text class="name" :class="{repuired: item.bixu == 1}">{{ item.vpName }}</text>
								<input type="text" :placeholder="'请选择'+item.vpName" v-model="item.value" class="flex"
									placeholder-class="pla" disabled />
								<text class="iconfont icon-xiangyouxiayiye"></text>
							</view>
						</timepicker>

						<!-- 6城市级联 -->
						<view class="item flex-box align-center" @click="showAddPicker(index)" v-if='item.vpType == 6'>
							<text class="name" :class="{repuired: item.bixu == 1}">{{ item.vpName }}</text>
							<input type="text" :placeholder="'请选择'+item.vpName" v-model="item.value" class="flex"
								placeholder-class="pla" disabled />
							<text class="iconfont icon-xiangyouxiayiye"></text>
						</view>
					</block>
					<mpvue-city-picker :shixian2='shixian2' ref="mpvueCityPicker_"
						:pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm"
						@onCancel='shixian2 = false'></mpvue-city-picker>
				</view>
				<button class="btn thmeBg" form-type="submit">修改信息</button>
				<button class="dropOut" @click="dropOut">退出</button>
				<!-- 	<view class="btn thmeBg" :style="{'--color': color}" @click="update">修改信息</view> -->
			</template>
		</form>
		<empty cat='4' :msg='errMsg' v-if='errMsg'></empty>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import timepicker from '../../components/time-picker.vue'
	import mpvueCityPicker from '../../components/mpvue-citypicker/mpvueCityPicker.vue'
	let that
	export default {
		components: {
			timepicker,
			mpvueCityPicker
		},
		data() {
			return {

				staticUrl: this.staticUrl,
				imgUrl: this.imgUrl,
				tempFilePath: '',
				userinfo: '',
				shixian2: false,
				cityPickerValueDefault: [0, 0, 0], // 默认选择地址
				current_index: -1,
				errMsg: '',
				v_headimg: '',
			};
		},
		onLoad() {
			that = this
			that.get_user_base_info()
		},
		methods: {
			...mapMutations(['getUser']),
			bindchooseavatar(e) {
				let headimg = e.detail.avatarUrl;
				// this.$common.uploadFile('/zzjApp/uploade?bid='+this.bid,headimg).then(res=>{
				// 	if(res.errcode != '100') return this.$Toast('上传失败')
				// 	let imgs = res.url.substring(22);
				// 	this.v_headimg = imgs;
				// })
				this.$common.uploadFiles(headimg).then(res => {
					if (res.errcode != '100') return this.$Toast('上传失败')
					let imgs = res.url.substring(22);
					this.v_headimg = imgs;
				})

			},
			get_user_base_info() {
				that.$http.get({
					url: '/singleSaleApi/user_base_info',
				}).then(res => {
					if (res.code != 200) return this.$Toast(res.msg)
					res.data.list.forEach(item => {
						if (item.vpType == 4) {
							let arry = []
							if (item.value)
								item.value = item.value.split(',')
							item.vpValue.forEach(vp => {
								let obj = {
									val: vp,
									checked: false
								}
								if (item.value) {
									item.value.forEach(vl => {
										if (vl == vp) {
											obj.checked = true
										}
									})
								}
								arry.push(obj)
							})
							item.vpValue = arry
						}
					})
					that.userinfo = res.data;
					that.v_headimg = res.data.v_headimg;
				})
			},
			btnConfirm(e) {
				if (this.userinfo.list[e.index].lock == 1) {
					uni.showToast({
						title: '暂不支持修改' + that.userinfo.list[e.index].vpName + ' !',
						icon: 'none'
					})
				} else {
					this.userinfo.list[e.index].value = e.key
				}
			},
			check(index, i) {
				if (that.userinfo.list[index].lock == 1) {
					uni.showToast({
						title: '暂不支持修改' + that.userinfo.list[index].vpName + ' !',
						icon: 'none'
					})
				} else {
					this.userinfo.list[index].vpValue[i].checked = !this.userinfo.list[index].vpValue[i].checked
				}
			},
			showAddPicker(index) {
				this.current_index = index;
				this.shixian2 = true;
			},
			onConfirm(e) {
				this.shixian2 = false;
				var add = e.label.replace(/-/g, ' ');
				this.userinfo.list[that.current_index].value = add
			},
			change(index, e) {
				if (that.userinfo.list[index].lock == 1) {
					uni.showToast({
						title: '暂不支持修改' + that.userinfo.list[index].vpName + ' !',
						icon: 'none'
					})
				} else {
					this.userinfo.list[index].value = this.userinfo.list[index].vpValue[e.detail.value]
				}
			},

			Save(e) {
				if (!e.detail.value.nickname) return this.$Toast('姓名不能为空!')
				this.userinfo.list[0].value = e.detail.value.nickname;
				this.update();
			},
			update() {
				let temp = that.userinfo.list;
				for (var index = 0; index < temp.length; index++) {
					var item = temp[index];
					if (item.vpType == 4) {
						if (item.vpValue) {
							let arry = []
							item.vpValue.forEach(ck => {
								if (ck.checked) {
									arry.push(ck.val)
								}
							})
							if (item.bixu == 1 && arry.length < 1) {
								uni.showToast({
									title: item.vpName + '不能为空！',
									icon: 'none'
								})
								return false;
							}
							item.value = arry.join(',')
						}
					} else {
						if (item.bixu == 1 && !item.value) {
							uni.showToast({
								title: item.vpName + '不能为空！',
								icon: 'none'
							})
							return false;
						}
					}
				}
				that.$http.post({
					url: '/singleSaleApi/edit_base_info',
					data: {

						property: JSON.stringify(temp),
						v_headimg: this.v_headimg
					}
				}).then(res => {
					if (res.code != 200) return this.$Toast(res.msg)
					this.$Toast(res.msg, 2)

				})
			},
			selectImage() { //选择图片
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'],
					success: (res) => {
						that.tempFilePath = res.tempFilePaths[0]
					}
				})
			},

			dropOut() {
				this.$Modal('提示', '是否确认退出?').then(res => {
					if (res) this.unbindDevice();
				})

			},

			unbindDevice() {
				this.$showLoading('');
				this.$http.post({
					url: '/zzj_singleSaleApi/unbindDevice',
				}).then(res => {
					if (res.errcode != 100) return this.$Toast(res.msg);
					this.$Toast(res.msg);
					this.getUser({
						vid: ''
					})
					this.$pop.pageRela('/pages/index/index')
				})
			}
		},
		computed: {
			...mapState(['bid', 'color']),
		}
	}
</script>

<style lang="scss" scoped>
	.Userimg {
		width: 104rpx;
		height: 104rpx;
		border-radius: 50%;
	}

	.AvatarBtn {
		position: absolute;
		width: 100%;
		height: 100%;
		left: 0;
		top: 0;
		z-index: 10;
		background-color: transparent !important;
		border-radius: 50%;
	}

	.AvatarBtn::after {
		border: none;
	}

	.page {
		background-color: #FFFFFF;
		padding-bottom: 60rpx;

		.avatar-box {
			width: 110rpx;
			height: 110rpx;
			margin: 30rpx auto;
			position: relative;

			.avatar {
				width: 100%;
				height: 100%;
				border-radius: 50%;
				box-shadow: 0 0 6rpx 1px #EEEEEE;
			}

			.icon-tubiao09 {
				font-size: 30rpx;
				position: absolute;
				bottom: 0;
				right: 6rpx;
			}
		}

		.list {
			padding: 30rpx;

			.name {
				font-size: 28rpx;
				position: relative;

				&::before {
					content: "*";
					color: red;
					position: absolute;
					left: -16rpx;
					display: none;
				}
			}

			.pla {
				color: #CCCCCC;
			}

			.item {
				border: 1px solid #EEEEEE;
				padding: 0 24rpx 0 30rpx;
				border-radius: 10rpx;
				height: 88rpx;
				line-height: 88rpx;
				margin-bottom: 30rpx;

				.flex {
					height: 100%;
					font-size: 28rpx;
					padding-left: 40rpx;
				}

				.icon-xiangyouxiayiye {
					color: #CCCCCC;
					font-size: 24rpx;
					font-weight: normal;
				}
			}

			.check-box {
				margin-bottom: 30rpx;
				padding-left: 30rpx;

				&-list {
					padding-left: 30rpx;
					flex-wrap: wrap;

					.check {
						display: flex;
						align-items: center;
						margin-right: 30rpx;

						.radio {
							width: 28rpx;
							height: 28rpx;
							border: 2rpx solid #bbbbbb;
							border-radius: 6rpx;
							margin-right: 8rpx;
							position: relative;
							display: flex;
							align-items: center;
							justify-content: center;

							.icon-xuanzhong {
								color: white;
								font-size: 24rpx;
							}
						}

						.txt {
							color: #666666;
							font-size: 28rpx;
						}

						&.active {
							.radio {
								background-color: var(--color);
								border: 2rpx solid var(--color);
							}

							.txt {
								color: #000000;
							}
						}

					}
				}
			}

			.textarea-box {
				margin-bottom: 30rpx;

				.name {
					margin-left: 30rpx;
				}

				textarea {
					border-radius: 10rpx;
					width: 100%;
					height: 200rpx;
					margin-top: 10rpx;
					font-size: 28rpx;
					padding: 24rpx;
					box-sizing: border-box;
					background-color: #F8F8F8;
				}
			}

			.repuired {
				&::before {
					display: block;
				}
			}

			.lock1 {
				color: #999999 !important;
			}
		}

		.btn {
			line-height: 88rpx;
			color: white;
			font-size: 28rpx;
			border-radius: 10rpx;
			text-align: center;
			margin: 60rpx 40rpx 0;
		}

		.dropOut {
			line-height: 88rpx;
			color: #999;
			font-size: 28rpx;
			border-radius: 10rpx;
			text-align: center;
			margin: 40rpx 40rpx 0;
			background-color: #f2f2f2;
		}
	}
</style>